#ifndef COMMONTYPES_H
#define COMMONTYPES_H

#include <QSerialPort>

const quint16 TDB_Address = 0xFF;

class CommonEnum : public QObject
{
    Q_OBJECT

public:
    /* 只读寄存器 */
    typedef enum {
        OptRead = 0x00,
        OptWrite,
    } RegisterOperatorType;
    Q_ENUM(RegisterOperatorType)

    // typedef enum {
    //     ReadOnly_ = 0x00,
    //         OptWrite,
    // } ReadOnlyRegisterAddressType;
    // Q_ENUM(RegisterAddressType)

};

typedef union {
    quint16 state;
    struct {
        quint16 WarningTotalVoltageOvervoltage          :1  ;   // 充放电端总电压过压告警
        quint16 WarningTotalVoltageUndervoltage         :1  ;   // 充放电端总电压低压告警
        quint16 WarningBatteryTotalVoltageOvervoltage   :1  ;   // 电池总电压过压告警
        quint16 WarningBatteryTotalVoltageUndervoltage  :1  ;   // 电池总电压低压告警
        quint16 WarningChargingOvercurrent              :1  ;   // 充电过流告警
        quint16 WarningDischargingOvercurrent           :1  ;   // 放电过流告警
        quint16 Reserved_1                              :1  ;   // 预留
        quint16 Reserved_2                              :1  ;   // 预留
        quint16 ChargingMOSHighTemprature               :1  ;   // 充电MOS管高温告警
        quint16 DischargingMOSHighTemprature            :1  ;   // 放电MOS管高温告警
        quint16 EnvinromentHighTemprature               :1  ;   // 环境温度高温告警
        quint16 Reserved_3                              :1  ;   // 预留
        quint16 WarningPowerReverseConnection           :1  ;   // 电源反接告警
        quint16 WarningShortCircuit                     :1  ;   // 短路保护
        quint16 Reserved_4                              :1  ;   // 预留
        quint16 Reserved_5                              :1  ;   // 预留
    };
} WarningUnion;

typedef union {
    quint16 state;
    struct {
        quint16 ProtectionTotalVoltageOvervoltage           :1  ;   // 充放电端总电压过压保护
        quint16 ProtectionTotalVoltageUndervoltage          :1  ;   // 充放电端总电压低压保护
        quint16 ProtectionBatteryTotalVoltageOvervoltage    :1  ;   // 电池总电压过压保护
        quint16 ProtectionBatteryTotalVoltageUndervoltage   :1  ;   // 电池总电压低压保护
        quint16 ProtectionChargingOvercurrent               :1  ;   // 充电过流保护
        quint16 ProtectionDischargingOvercurrent            :1  ;   // 放电过流保护
        quint16 Reserved_1                                  :1  ;   // 预留
        quint16 Reserved_2                                  :1  ;   // 预留
        quint16 ProtectionChargingMOSHighTemprature         :1  ;   // 充电MOS管高温保护
        quint16 ProtectionDischargingMOSHighTemprature      :1  ;   // 放电MOS管高温保护
        quint16 ProtectionEnvinromentHighTemprature         :1  ;   // 环境温度高温保护
        quint16 Reserved_3                                  :1  ;   // 预留
        quint16 ProtectionPowerReverseConnection            :1  ;   // 电源反接保护
        quint16 ProtectionShortCircuit                      :1  ;   // 短路保护
        quint16 ProtectionInductorCurrentUnevenFlow         :1  ;   // 电感电流不均流保护
        quint16 ProtectionModuleOverload                    :1  ;   // 模块重载保护
    };
} ProtectionUnion;

typedef union {
    quint16 state;
    struct {
        quint16 StateBUCKCurrentLimitingCharge              :1  ;   // BUCK 限流充电
        quint16 StateThroughCharge                          :1  ;   // 直通充电
        quint16 StateThroughDischarge                       :1  ;   // 直通放电
        quint16 StateBUCKCurrentLimitingDischarge           :1  ;   // BUCK限流放电
        quint16 StateBOOSTCurrentLimitingDischarge          :1  ;   // BOOST限流放电
        quint16 StateStandBy                                :1  ;   // 静置待机
        quint16 StateProtection                             :1  ;   // 保护状态
        quint16 Reserved_1                                  :1  ;   // 预留
        quint16 StateRalayOn                                :1  ;   // 继电器导通
        quint16 StateDischargeMainMOSOn                     :1  ;   // 放电主 MOS 管开启
        quint16 StateChargeMainMOSOn                        :1  ;   // 充电主 MOS 管开启
        quint16 Reserved_2                                  :1  ;   // 预留
        quint16 Reserved_3                                  :1  ;   // 预留
        quint16 Reserved_4                                  :1  ;   // 预留
        quint16 Reserved_5                                  :1  ;   // 预留
        quint16 Reserved_6                                  :1  ;   // 预留
    };
} WorkStateUnion;

/* 只读寄存器 */
typedef struct {
    quint16         TotalVoltage;               // 充放电端总电压
    qint16          TotalCurrent;               // 充放电端总电流
    qint16          TotalPower;                 // 充放电端总功率
    quint16         TotalBatteryVoltage;        // 电池总电压
    qint16          TotalBatteryCurrent;        // 电池总电流
    qint16          TDBMaxTemprature;           // TDB最高温度
    qint16          ChargingMOSTemprature;      // 充电 MOS 管温度
    qint16          DischargingMOSTemprature;   // 放电 MOS 管温度
    qint16          EnvinromentTemprature;      // 环境温度
    WarningUnion    WarningState;               // 告警状态位
    ProtectionUnion ProtectionState;            // 保护状态位
    quint16         Reserved;                   // 预留
    WorkStateUnion  TDBWorkState;               // TDB工作状态
} READ_ONLY_REG;



typedef struct {
    QString                     portName;           // 串口名称
    QSerialPort::BaudRate       baudrate;           // 串口波特率
    QSerialPort::DataBits       dataBits;           // 串口数据位
    QSerialPort::Parity         parity;             // 串口校验位
    QSerialPort::StopBits       stopBits;           // 串口停止位
    QSerialPort::FlowControl    flowControl;        // 串口流控
} SerialPortSettings;

#endif // COMMONTYPES_H
